package org.personal.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.myhibernate.ProxyTemplate;
import org.myhibernate.Template;
import org.myhibernate.core.method.PageSizeQuery;
import org.personal.Constants;
import org.personal.entity.Layout;
import org.personal.entity.Page;
import org.personal.entity.User;
import org.personal.entity.UserPage;
import org.personal.util.UserUtil;

import com.util.ConfigUtil;
import com.util.IOUtil;
/**
 * 1、获取到页面，如果页面不存在，则跳转到默认页面，没有指定默认页面，就跳转到index.jsp页面
 * 2、获取用户，没有用户就是匿名用户
 * 3、获取页面布局
 * 4、获取页面的portlets
 * 
 * @author Administrator
 *
 */
public class PortalServlet extends HttpServlet
{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
	{
		//System.out.println("##############################3PortalServlet pageId="+request.getParameter("pageId"));
		
		if (ConfigUtil.getAttribute(Constants.CONTEXT)==null)
		{
			ConfigUtil.setAttribute(Constants.CONTEXT, request.getRealPath(""));
		}
		String baseProjectPath=ConfigUtil.getAttribute(Constants.CONTEXT);
		//System.out.println("context.path="+ConfigUtil.getAttribute(Constants.CONTEXT));
		
		String pageId=request.getParameter("pageId");
		Page page=null;
		User user=(User) request.getSession().getAttribute(Constants.USER);
		UserPage userPage=null;
		Layout layout=null;
		List<String> portlets=null;
		String pagePath="index.jsp";
		boolean editable=false;
		
		if (ConfigUtil.getAttribute(Constants.DEFAULT_PAGE)!=null)
		{
			pagePath=ConfigUtil.getAttribute(Constants.DEFAULT_PAGE);
		}
		UserUtil userUtil=(UserUtil) request.getSession().getAttribute(Constants.USER_UTIL);
		if (userUtil==null)
		{
			userUtil=new UserUtil();
			request.getSession().setAttribute(Constants.USER_UTIL,userUtil);
		}
		if ((pageId!=null)&&(!"".equals(pageId))&&(!"null".equalsIgnoreCase(pageId)))
		{
			Template<Page> template_page=new ProxyTemplate(Page.class).getInstance();
			Template<Layout> template_layout=new ProxyTemplate(Layout.class).getInstance();
			Template<UserPage> template_userPage=new ProxyTemplate(UserPage.class).getInstance();
			try 
			{
				//获取页面，及其页面对用户的权限处理
				page=template_page.get(Integer.parseInt(pageId));
				if (page!=null)
				{
					//获取页面路径
					pagePath=page.getPagePath();
					
					//用户已经登录
					if (user != null)
					{
						//用户已经登录
						List<String> groups=userUtil.copyList(user.getGroups());
						groups.add(Constants.USER_ALLUSER);
						groups.add(user.getUid());
						//是否有查看权限
						if (userUtil.hasCommon(page.getUsers(), groups))
						{
							//获取页面的默认布局
							if ((page.getLayout()!= -1))
							{
								layout=template_layout.get(page.getLayout());
							}
							//获取页面的默认portlet
							portlets=page.getPortlets();
							//System.out.println("editUsers="+page.getEditUsers());
							//System.out.println("groups="+groups);
							//是否有编辑权限
							editable=userUtil.hasCommon(page.getEditUsers(), groups);
							
							//是否有自己的个性化
							PageSizeQuery<UserPage> query_userPage=template_userPage.getPageSizeQuery();
							int count=query_userPage.query(" where userId='"+user.getUid()+"' and pageId="+pageId+" ");
							if (count>0)
							{
								userPage=query_userPage.getListObject(0, 1).get(0);
								if ((userPage.getLayoutId()!= -1))
								{
									layout=template_layout.get(userPage.getLayoutId());
								}
								portlets=userPage.getPortlets();
							}
						}
					}else 
					{
						//匿名用户
						List<String> userList=new ArrayList<String>();
						userList.add(Constants.USER_ANONYMOUS);
						if (userUtil.hasCommon(page.getUsers(), userList))
						{
							//获取页面的默认布局
							if ((page.getLayout()!= -1))
							{
								layout=template_layout.get(page.getLayout());
							}
							//获取页面的默认portlet
							portlets=page.getPortlets();
						}
					}//endif
					
					//获取默认布局
					if (layout==null)
					{
						PageSizeQuery<Layout> query=template_layout.getPageSizeQuery();
						int count=query.query(" where defaultLayout='true' ");
						if (count>0)
						{
							layout=query.getListObject(0,1).get(0);
						}
					}
					
					//读取布局的html代码
					if ((layout!=null)&&("".equals(layout.getHtml())))
					{
						//读取layout html
						IOUtil ioUtil=new IOUtil();
						String layoutHtml=ioUtil.read(baseProjectPath+layout.getPagePath(), "utf-8", "");
						layout.setHtml(layoutHtml);
					}
				}//endif page!=null
			} catch (Exception e) 
			{
				e.printStackTrace();
			}finally{
				template_page.close();
				template_layout.close();
				template_userPage.close();
			}
		}
		//display(request);
		request.setAttribute(Constants.PAGE,page);
		request.getSession().setAttribute(Constants.PAGE+"_"+pageId,page);
		request.setAttribute(Constants.LAYOUT,layout);
		request.setAttribute(Constants.PORTLETS,portlets);
		request.setAttribute(Constants.USER_PAGE,userPage);
		request.setAttribute(Constants.EDITABLE,editable);
		request.getRequestDispatcher(pagePath).forward(request,response);
	}
	public void display(HttpServletRequest request)
	{
		Enumeration<String> enus= request.getSession().getAttributeNames();
		System.out.println("=====================================================");
		while(enus.hasMoreElements())
		{
			String key=enus.nextElement();
			System.out.println(key+"="+request.getSession().getAttribute(key));
		}
		System.out.println("=====================================================");
	}
	
}
